package com.chenjun.core.service.oa.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModel;
import com.chenjun.common.util.DateUtil;
import com.chenjun.core.dao.generator.entity.OaBillAttnDayReport;
import com.chenjun.core.dao.generator.entity.OaBillAttnDetail;
import com.chenjun.core.dao.generator.mapper.OaBillAttnDayReportMapper;
import com.chenjun.core.dao.module.OaBillAttnDayReportRelationMapper;
import com.chenjun.core.service.oa.OaBillAttnDayReportService;

@Service
@Transactional
@SuppressWarnings("serial")
public class OaBillAttnDayReportServiceImpl implements OaBillAttnDayReportService {

	@Autowired
	private OaBillAttnDayReportMapper dayReportMapper;
	@Autowired
	private OaBillAttnDayReportRelationMapper dayReportRelationMapper;
	
	@Override
	public OaBillAttnDayReport get(int id) {
		return dayReportMapper.selectByPrimaryKey(id);
	}

	@Override
	public int create(OaBillAttnDayReport oaBillAttnDayReport) {
		return dayReportMapper.insertSelective(oaBillAttnDayReport);
	}

	@Override
	public int delete(int id) {
		return dayReportMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int update(OaBillAttnDayReport oaBillAttnDayReport) {
		return dayReportMapper.updateByPrimaryKey(oaBillAttnDayReport);
	}

	@Override
	public int createOrUpdate(OaBillAttnDayReport report) {
		int rows = 0;
		if(StringUtils.isBlank(report.getId().toString())){
			rows = dayReportMapper.insertSelective(report);
		}else{
			rows = dayReportMapper.updateByPrimaryKeySelective(report);
		}
		return rows;
	}

	@Override
	public List<OaBillAttnDayReport> doListByCondition(DataGridModel param) {
		return dayReportRelationMapper.selectByCondition(param);
	}

	@Override
	public Pagination doPagination(DataGridModel param) {
		Pagination vo = new Pagination();
		List<OaBillAttnDayReport> list = dayReportRelationMapper.selectByCondition(param);
		param.setStart(0);
		param.setRows(0);
		int size = dayReportRelationMapper.countByCondition(param);
		vo.setAaData(list);
		vo.setiTotalRecords(size);
		vo.setsEcho(param.getPage());
		vo.setiTotalDisplayRecords(size);
		return vo;
	}

	@Override
	public List<OaBillAttnDayReport> getDistinctByEmplid(DataGridModel param) {
		return dayReportRelationMapper.getDistinctByEmplid(param);
	}
	
	/**
	 * @Description: 更新日报的正常工作小时数 
	 * @param oaBillAttnDayReport
	 * @param normalHours
	 * @return
	 * @author dongyibin
	 * @date 2017-9-18 下午11:47:59
	 */
	@Override
	public boolean updateNormalHours(OaBillAttnDayReport oaBillAttnDayReport, float normalHours){
		oaBillAttnDayReport.setNormalHours(normalHours);
		oaBillAttnDayReport.setChangeNormalHours(normalHours);
		return createOrUpdate(oaBillAttnDayReport) > 0;
	} 
	
	/**
	 * @Description: 创建新的日报
	 * @param oaBillAttnDetail
	 * @param normalHours
	 * @return
	 * @author dongyibin
	 * @date 2017-9-18 下午11:51:12
	 */
	@Override
	public boolean createDayReport(OaBillAttnDetail oaBillAttnDetail, float normalHours){
		OaBillAttnDayReport dayReport = new OaBillAttnDayReport();
		dayReport.setNo(oaBillAttnDetail.getNo());
		dayReport.setBillDate(oaBillAttnDetail.getBillDate());
		dayReport.setCreateTime(new Date());
		dayReport.setModifyTime(new Date());
		dayReport.setYear(DateUtil.getYear(oaBillAttnDetail.getBillDate()));
		dayReport.setMonth(DateUtil.getMonth(oaBillAttnDetail.getBillDate()));
		dayReport.setEmplId(oaBillAttnDetail.getEmplId());
		dayReport.setEmplCode(oaBillAttnDetail.getEmplCode());
		dayReport.setEmplName(oaBillAttnDetail.getEmplName());
		dayReport.setManHoursTypeId(oaBillAttnDetail.getManHoursTypeId());
		dayReport.setManHoursTypeName(oaBillAttnDetail.getManHoursTypeName());
		dayReport.setNormalHours(normalHours);
		dayReport.setChangeNormalHours(normalHours);
		dayReport.setOvertimeHours(0f);
		dayReport.setChangeOvertimeHours(0f);
		dayReport.setAttnTypeId(oaBillAttnDetail.getAttnTypeId());
		dayReport.setAttnTypeName(oaBillAttnDetail.getAttnTypeName());
		int create = create(dayReport);
		return create > 0;
	}
	
}
